<?php

class Video extends Admin_Controller {

    function Video() {
        parent::Admin_Controller();
        $this->load->model('Video_m');
        $this->load->helper('date');
    }

    function index() {
        $this->liste_video();
    }

    function ajouter_video() {
        is_admin();
        
        $config = array(
            array(
                'field' => 'name',
                'label' => 'videoname',
                'rules' => 'required'
            )
        );
        $this->form_validation->set_rules($config);
        if ($this->input->post('submit')) {
            if ($this->form_validation->run()) {
                $config = array(
                    'allowed_types' => "flv|mp4|wma",
                    'upload_path' => './assets/videos/',
                    'max_size' => 512000,
                    'encrypt_name' =>TRUE
                );
                $this->load->library('upload', $config);
                if (!$this->upload->do_upload()) {
                    $this->_data['upload_error'] = $this->upload->display_errors();
                    $this->display_view('video/ajouter_video');
                } else {
                    $upload_data = $this->upload->data();
                    if ($this->form_validation->run()) {
                        $data = array(
                            'name' => $this->input->post('name'),
                            'description' => $this->input->post('description'),
                            'upload_time' => time(),
                            'upload_by' => $this->session->userdata('adminid'),
                            'file_name' => $upload_data['file_name']
                        );
                        $this->Video_m->upload_video($data);
                        redirect('video/liste_video');
                    } else {
                        $this->display_view('video/ajouter_video');
                    }
                }
            } else {
                $this->display_view('video/ajouter_video');
            }
        } else {
            $this->display_view('video/ajouter_video');
        }
    }

    function liste_video() {
        is_admin();
        $per_page = $this->uri->segment(3);
        if ($per_page == "") {
            $per_page = 7;
        }
        $off_set = $this->uri->segment(4);
        if ($off_set == "") {
            $off_set = 0;
        }
        $config['base_url'] = base_url() . 'index.php/video/liste_video/' . '/' . $per_page . '/';
        $nav_link = $this->session->set_userdata('smart_navigate_url',  site_url('video/liste_video')) ;
        $config['per_page'] = '7';
        $config['uri_segment'] = 4;
        $config['total_rows'] = $this->db->count_all('seating_videos');
        if ($off_set > 0 && $off_set == $config['total_rows']) {
            $off_set = $off_set - $per_page;
        }
        $this->_data['query'] = $this->Video_m->get_all_video($config['per_page'], $off_set);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['total'] = $this->db->count_all('seating_videos');
        $this->_data['page_title'] = "Liste de video";
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        $this->display_view('video/liste_video');
    }

    function delete_video($id, $per_page, $off_set) {
        is_admin();

        $this->Video_m->delete_video_comment($id);

        $video_by_id = $this->Video_m->get_video_by_id($id, $per_page, $off_set);
        if ($video_by_id->num_rows() > 0) {
            $video = $video_by_id->row();
            $file_name = './assets/videos/' . $video->file_name;
            if ($video->file_name != null) {
                delete_pro_file($file_name);
            }
        }

        $this->Video_m->delete_video($id);

        redirect(site_url('video/liste_video') . '/' . $per_page . '/' . $off_set);
    }

    function edit_video($id) {
        $video = $this->Video_m->get_video_by_id($id);
        $this->_data['video'] = $video;

        $this->_data['erreur'] = "";
        $config = array(
            array(
                'field' => 'name',
                'label' => 'Display name',
                'rules' => 'required'
            ),
            array(
                'field' => 'description',
                'Label' => 'Description',
                'rules' => 'required'
            )
        );
        $this->form_validation->set_rules($config);
        if ($this->input->post('submit')) {
            if ($this->form_validation->run()) {
                $data = array(
                    'name' => $this->input->post('name'),
                    'description' => $this->input->post('description'),
                    'upload_time' =>  time()
                );
                $this->Video_m->edit_video($data, $id);
                redirect('video/liste_video');
            } else {
                $this->display_view('video/edit_video');
            }
        } else {
                $this->display_view('video/edit_video');
        }
    }

}

?>
